package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-19 14:09
 * @Version 1.0
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    /*@RequestMapping
    public String findAll(Model model){
        List<Role> roleList = this.roleService.findAll();
        model.addAttribute("roleList",roleList);
        return "role/index";
    }*/

    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping
    public String toIndex(Model model, HttpServletRequest request){
        Map<String,Object> filters = getFilters(request);
        PageInfo<Role> page = roleService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "role/index";
    }


    @PreAuthorize("hasAuthority('role.create')")
    @GetMapping("/create")
    public String create(){
        return "role/create";
    }

    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/save")
    public String save(Role role){
        roleService.insert(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id){
        Role role = roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/update")
    public String update(Role role){
        roleService.update(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        roleService.delete(id);
        return "redirect:/role";
    }


    // /role/assignShow/"+id  进入分配权限页面
    @PreAuthorize("hasAuthority('role.assign')")
    @RequestMapping("assignShow/{roleId}")
    public String assignShow(Model model,@PathVariable Long roleId){
        List<Map<String,Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes",zNodes);
        model.addAttribute("roleId",roleId);
        return "role/assignShow";
    }

    //@{/role/assignPermission}给角色分配权限
    @PreAuthorize("hasAuthority('role.assign')")
    @RequestMapping("assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRelationship(roleId,permissionIds);
        return "common/successPage";
    }

}
